/**
 * ConstraintTypeComboBoxItemListener.java
 * 2012-2-21 下午10:45:16
 */
package xgenerator.ui.swing.adapter;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import xgenerator.ui.swing.MetadataTable;
import xgenerator.ui.swing.model.ConstraintTypeComboBoxModel;
import xgenerator.ui.swing.model.MetadataTableModel;

/**
 * <p>
 * Title:类的中文名
 * </p>
 * <p>
 * Description:具体功能见各方法描述
 * </p>
 * <p>
 * Copyright:Copyright (c) 2012
 * </p>
 * 
 * @author <a href="mailto:lishushan@gmail.com">liss</a>
 * @version 1.0
 */
public class ConstraintTypeComboBoxItemListener implements ItemListener {
	

	/**
	 * UI组件
	 */
	private Component ui;
	
	/**
	 * constructor
	 * @param ui
	 */
	public ConstraintTypeComboBoxItemListener(Component ui) {
		this.ui = ui;
	}

	/**
	 * <p>
	 * Title：约束类型下拉列表项改变
	 * </p>
	 * @param e
	 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			Object selectedItem = e.getItem();
			String keyType = null != selectedItem ? selectedItem.toString() : null;
			MetadataTable metadataTable = (MetadataTable) ui;
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			int selectedRowIndex = metadataTable.getSelectedRow();
			
			if(ConstraintTypeComboBoxModel.FK_TYPE.equals(keyType)) {
				metadataTableModel.setCellEditableAt(true, selectedRowIndex, MetadataTableModel.REFERENCING_TABLE_INDEX);
				metadataTableModel.setCellEditableAt(true, selectedRowIndex, MetadataTableModel.REFERENCING_COLUMN_INDEX);
				metadataTableModel.setCellEditableAt(true, selectedRowIndex, MetadataTableModel.DELETE_RULE_INDEX);
			//} else if(ConstraintTypeComboBoxModel.PK_TYPE.equals(keyType) || ConstraintTypeComboBoxModel.UK_TYPE.equals(keyType) || ConstraintTypeComboBoxModel.CHECK_TYPE.equals(keyType) || ConstraintTypeComboBoxModel.NOT_NULL_TYPE.equals(keyType) || ConstraintTypeComboBoxModel.DEFAULT_TYPE.equals(keyType)) {
			} else {
				metadataTableModel.setCellEditableAt(false, selectedRowIndex, MetadataTableModel.REFERENCING_TABLE_INDEX);
				metadataTableModel.setCellEditableAt(false, selectedRowIndex, MetadataTableModel.REFERENCING_COLUMN_INDEX);
				metadataTableModel.setCellEditableAt(false, selectedRowIndex, MetadataTableModel.DELETE_RULE_INDEX);
				metadataTableModel.setValueAt(null, selectedRowIndex, MetadataTableModel.REFERENCING_TABLE_INDEX);
				metadataTableModel.setValueAt(null, selectedRowIndex, MetadataTableModel.REFERENCING_COLUMN_INDEX);
				metadataTableModel.setValueAt(null, selectedRowIndex, MetadataTableModel.DELETE_RULE_INDEX);
			}
		}
	}

}
